package ipgather.core.redis;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import ipgather.constant.GlobalConstant;
import ipgather.entity.Host;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class RedisDbAdapter {

	private static Jedis jedis = null;
	private static RedisDbAdapter redisDbAdapter = new RedisDbAdapter();
	private static final String REDIS_HOST = "redis.host";// 主机地址
	private static final String REDIS_PORT = "redis.port";// 端口
	private static final String REDIS_AUTH = "redis.auth";// 密码
	private static JedisPool jedisPool = null;

	static {
		//collect();
		jedisPool = getPool();
	}
	
	private static JedisPool getPool(){
		Properties properties = new Properties();
		if(null==jedisPool){
			synchronized (RedisDbAdapter.class) {
				if(null==jedisPool){
					String pat = RedisDbAdapter.class.getResource("/").getPath();
					try {
						properties.load(new FileInputStream(pat+"redis.properties"));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					JedisPoolConfig config = new JedisPoolConfig();
					config.setMaxTotal(100);
					config.setMaxIdle(100);//最多有多少个空闲jedis
					config.setMaxWaitMillis(1000*100);
					config.setTestOnBorrow(true);
					String auth = properties.getProperty(REDIS_AUTH);
					if(StringUtils.isNotBlank(auth)){
						jedisPool = new JedisPool(config,properties.getProperty(REDIS_HOST),Integer.parseInt(properties.getProperty(REDIS_PORT)),10000,auth);
					}else{
						jedisPool = new JedisPool(config,properties.getProperty(REDIS_HOST),Integer.parseInt(properties.getProperty(REDIS_PORT)),10000);
					}
				}
			}
		}
		return jedisPool;
	}
	
	private static void collect(){
		Properties properties = new Properties();
		try {
			String pat = RedisDbAdapter.class.getResource("/").getPath();
			properties.load(new FileInputStream(pat+"redis.properties"));
			jedis = new Jedis(properties.getProperty(REDIS_HOST), Integer.parseInt(properties.getProperty(REDIS_PORT)));
			String auth = properties.getProperty(REDIS_AUTH);
			if(StringUtils.isNotBlank(auth)){
				jedis.auth(auth);
			}
		} catch (FileNotFoundException e) {
			System.err.println("redis配置文件不存在\n");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static RedisDbAdapter create() {
		return redisDbAdapter;
	}
	
	public Jedis getJedis(){
		return jedisPool.getResource();
	}
	
	/**
	 * 保存普通数据
	 * @param key 键
	 * @param val 值
	 */
	public void save(String key,String val){
		if(GlobalConstant.REDIS_TEST){
			System.out.println("redis保存数据："+key+":"+val);
		}else{
			jedisPool.getResource().set(key, val);
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				jedis.set(key,val);
			}catch(JedisConnectionException jce){
				
			} finally {
				if(null!=jedis){
					jedis.close();
				}
			}
		}
	}
	
	/**
	 * 删除普通数据
	 * @param key 键
	 * @param val 值
	 */
	public void delHost(Host host){
		if(GlobalConstant.REDIS_TEST){
			System.out.println("redis删除数据："+host.getHn()+":"+host.getP());
		}else{
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				jedis.del(host.getHn()+":"+host.getP());
			}catch(JedisConnectionException jce){
				
			} finally {
				if(null!=jedis){
					jedis.close();
				}
			}
		}
	}
	
	/**
	 * 判断主机是否存在 
	 * @param host
	 * @return
	 */
	public boolean hostExist(Host host){
		Jedis jedis = null;
		boolean exist = false;
		try {
			jedis = jedisPool.getResource();
			exist = jedis.exists(host.getHn()+":"+host.getP());;
		}catch(JedisConnectionException jce){
			
		} finally {
			if(null!=jedis){
				jedis.close();
			}
		}
		return exist;
	}
	
	/**
	 * 保存Host主机地址数据
	 * @param host 主机地址
	 * @param port 端口
	 * <pre>
     *	此处方法数据组装已经进行封装.
     *	key = host:port
 	 *	val = { hn:.., p:.. }
	 * </pre>
	 */
	public void saveHost(String host,int port){
		if(GlobalConstant.REDIS_TEST){
			System.out.println("redis保存数据："+host+":"+port);
		}else{
			Jedis jedis = null;
			try {
				jedis = jedisPool.getResource();
				jedis.set(new StringBuffer(host).append(":").append(port).toString(), JSON.toJSONString(new Host(host,port)));
			}catch(JedisConnectionException jce){
				
			} finally {
				if(null!=jedis){
					jedis.close();
				}
			}
		}
	}

}
